<?php

require_once 'IOperations.php';
require_once 'Connexion.class.php';
require_once '../modele/Activite.class.php';
require_once '../modele/Tache.class.php';
require_once '../modele/Utilisateur.class.php';

class AccesDonneesTache implements IOperations
{
    private $tache;
    private $utilisateur;
    private $activite;
    
    public function __construct ($tache, $utilisateur, $activite = null)
    {
	$this->tache = $tache;
	$this->utilisateur = $utilisateur;
	$this->activite = $activite;
	try
	{
	    $this->initConnexion = new Connexion();
	    $this->connexionBDD = $this->initConnexion->connexionBDD();
	}
	catch (PDOException $erreur)
	{
	    echo 'Erreur, classe : ' . __CLASS__ .  ', méthode : ' . __METHOD__;
	}
    }
    
    public function creation()
    {
	$refActivite = $this->tache->getTchActivite();
	$id = $refActivite->getActvID();
	
	$dateCreation = $this->tache->getTchDateCreation();
	$dateEcheance = $this->tache->getTchDateEcheance();
	$label = $this->tache->getTchLabel();
	$statut = $this->tache->getTchStatut();
	
	$requetePreparee = $this->connexionBDD->prepare("INSERT INTO s_gestion_projet.v_infos_t_tache_tch
			    (actv_id, tch_date_creation, tch_date_echeance, tch_label, tch_statut)
			    VALUES (?, ?, ?, ?, ?)");
	
	$requetePreparee->bindParam(1,$id);
	$requetePreparee->bindParam(2,$dateCreation);
	$requetePreparee->bindParam(3,$dateEcheance);
	$requetePreparee->bindParam(4,$label);
	$requetePreparee->bindParam(5,$statut);
	
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
    
    public function selection()
    {
	$id = $this->utilisateur->getUsrID();

	$requetePreparee = $this->connexionBDD->prepare("SELECT tch_id,
								actv_id,
								actv_label,
								to_char(tch_date_creation,'DD-MM-YYYY HH24:MI:SS') AS tch_date_creation,
								to_char(tch_date_echeance,'DD-MM-YYYY HH24:MI:SS') AS tch_date_echeance,
								tch_label,
								tch_statut FROM s_gestion_projet.v_infos_t_tache_tch_utilisateur_usr
							WHERE usr_id = ? ORDER BY actv_label, tch_id");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	$tableauTacheSelection = $resultatSQL;
	
	return $tableauTacheSelection;
    }
    
    public function selectionGrouperParActivite()
    {
	$id = $this->tache->getTchActivite();
	
	$requetePreparee = $this->connexionBDD->prepare("SELECT * FROM s_gestion_projet.v_infos_t_tache_tch
							WHERE actv_id = ?");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	$tableauTacheSelection = array(array());
    
	$i = 0;
	foreach ($resultatSQL as $ligne)
	{   
	    $tableauTacheSelection['tch_id'] = $ligne['tch_id'];
	    $tableauTacheselection['actv_id'] = $ligne['actv_id'];
	    $tableauTacheSelection['tch_date_creation'] = $ligne['tch_date_creation'];
	    $tableauTacheSelection['tch_date_echeance'] = $ligne['tch_date_echeance'];
	    $tableauTacheSelection['tch_label'] = $ligne['tch_label'];
	    $tableauTacheSelection['tch_statut'] = $ligne['tch_statut'];
	
	    $i++;
	}
	
	return $tableauTacheSelection;
    }
    
    public function suppression()
    {
	$id = $this->tache->getTchID();
				
	$requetePreparee = $this->connexionBDD->prepare("DELETE FROM s_gestion_projet.v_infos_t_tache_tch
							WHERE tch_id = ?");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
    
    public function miseAJour()
    {
	$dateCreation = $this->tache->getTchDateCreation();
	$dateEcheance = $this->tache->getTchDateEcheance();
	$label = $this->tache->getTchLabel();
	$statut = $this->tache->getTchStatut();
	$idActivite = $this->activite->getActvID();
	$idTache = $this->tache->getTchID();
	
	$requetePreparee = $this->connexionBDD->prepare("UPDATE s_gestion_projet.v_infos_t_tache_tch
				SET tch_date_creation = ?,
				tch_date_echeance = ?,
				tch_label = ?,
				tch_statut = ?
				WHERE actv_id = ? AND tch_id = ?");
	
	$requetePreparee->bindParam(1,$dateCreation);
	$requetePreparee->bindParam(2,$dateEcheance);
	$requetePreparee->bindParam(3,$label);
	$requetePreparee->bindParam(4,$statut);
	$requetePreparee->bindParam(5,$idActivite);
	$requetePreparee->bindParam(6,$idTache);	
	    		
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
}
?>


